package com.qs.eggs.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.RatingBar;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.bean.CollectBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SubEvaluationdapter extends SimpleAdapter<CollectBean> implements BaseAdapter.OnItemClickListener {
    //定义一个HashMap，用来存放EditText的值，Key是position
    HashMap<Integer, String> hashMap = new HashMap<Integer, String>();
    HashMap<Integer, String> hashMap1 = new HashMap<Integer, String>();
   // private RatingBar ratingBar;
    private TextView mComment;
    private List<CollectBean> datas;
    private ArrayList<HashMap<String,String>> mCommentList;
    private ArrayList<HashMap<String,String>> mInfo;
    BaseViewHolder viewHoder;
    public SubEvaluationdapter(Context context, List<CollectBean> datas) {
        super(context, R.layout.item_sub_evaluation, datas);
        mCommentList=new ArrayList<>();
        this.datas=datas;
    }

    @Override
    protected void convert(BaseViewHolder viewHoder, final CollectBean item, final int position) {
        this.viewHoder=viewHoder;
        TextView mComment= viewHoder.getTextView(R.id.et_comment);
        TextView proname=(TextView)viewHoder.getView(R.id.proname);
        RatingBar ratingBar=(RatingBar)viewHoder.getView(R.id.room_ratingbar);
        this.mComment=mComment;
     //   this.ratingBar=ratingBar;
        SimpleDraweeView draweeView = (SimpleDraweeView) viewHoder.getView(R.id.propic);
        draweeView.setImageURI(Constants.URL_BASE+"productimages/"+item.getAutoname());
        proname.setText(item.getProname());

        int rating=0;
        ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                rating=(int) Math.rint(ratingBar.getRating());
                hashMap1.put(position,rating+"");
            }
        });
        mComment.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                //将editText中改变的值设置在HashMap中
                hashMap.put(position, s.toString());
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start,
                                          int count,int after) {

            }

        });

        //如果hashMap不为空，就设置的editText
        if(hashMap.get(position) != null){
            mComment.setText(hashMap.get(position));
        }

        int scre=(int) Math.rint(ratingBar.getRating());
        HashMap<String,String> map=new HashMap<>();
        map.put("comment",mComment.getText().toString());
        map.put("score",scre+"");
        mCommentList.add(map);

    }

    @Override
    public void onItemClick(View view, int position) {

    }

    public ArrayList<HashMap<String,String>> getInfo(){

    if (TextUtils.isEmpty(mComment.getText().toString())) {
        mInfo = null;
    }else {
        mInfo=new ArrayList<>();
        for (int i=0;i<datas.size();i++) {
        //    TextView comtent=(TextView) viewHoder.views.get(i).findViewById(R.id.et_comment);
            HashMap<String, String> map = new HashMap<>();

                map.put("proid", datas.get(i).getProid());
                map.put("proname", datas.get(i).getProname());
                map.put("type", datas.get(i).getType());
                map.put("comments",hashMap.get(i));
                map.put("scores",hashMap1.get(i));
                mInfo.add(map);
            }
        }
        return mInfo;
    }
}
